package com.kehao.leetcode.basic.dp;

import org.junit.Test;

public class DPSolution2 {

    public int maxProfit(int[] prices) {
        //数组实现
//        int[] maxProfitWithoutStock = new int[prices.length];
//        int[] maxProfitWithStock = new int[prices.length];
//        maxProfitWithoutStock[0] = 0;
//        maxProfitWithStock[0] = -prices[0];
//        for (int i = 1; i < prices.length; i++) {
//            maxProfitWithoutStock[i] = Math.max(maxProfitWithoutStock[i-1],maxProfitWithStock[i-1]+prices[i]);
//            maxProfitWithStock[i] = Math.max(maxProfitWithStock[i-1],-prices[i]);
//        }
//        return Math.max(maxProfitWithoutStock[prices.length-1],maxProfitWithStock[prices.length-1]);

        //使用变量
        int maxProfitWithoutStock = 0;
        int maxProfitWithStock = -prices[0];
        for (int i = 1; i < prices.length; i++) {
            maxProfitWithoutStock = Math.max(maxProfitWithoutStock, maxProfitWithStock + prices[i]);
            maxProfitWithStock = Math.max(maxProfitWithStock, -prices[i]);
        }
        return maxProfitWithoutStock;
    }

    @Test
    public void test01(){
        System.out.println(maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
    }
}
